package com.itheima.service.imp;

import com.itheima.mapper.DeptLogMapper;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Dept;
import com.itheima.pojo.DeptLog;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;

    @Override
    public List<Dept> list() {
        return deptMapper.list();
    }

    @Override
    public void save(Dept dept) {
        System.out.println("部门名称" + dept.getName());
        deptMapper.insert(dept);
    }

    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private DeptLogServiceImpl deptLogService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(Integer id) throws FileNotFoundException {
        String msg = "";
        try {
            deptMapper.delete(id);

            empMapper.deleteByDId(id);
        } catch (Exception e) {
            msg = "好像失败了";
            throw  e;
        } finally {
            DeptLog log = new DeptLog();
            log.setDescription(id + "部门被删除了" + msg);
            log.setCreateTime(LocalDateTime.now());
            deptLogService.insert(log);
        }


    }

    @Override
    public Dept getById(Integer id) {
        return deptMapper.getById(id);
    }

    @Override
    public void updateById(Dept dept) {
        deptMapper.update(dept);
    }

}
